import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { message } from "antd";
import {getlist,getcomment,commshow,getdelete,getpush,getedit,editdel} from '@/api/work'
export interface IndexModelState {
  name: string;
}



const IndexModel: any = {
  namespace: 'work',

  state: {
   list:[],
   comm:[]
  },

  effects: {
    //列表
    *getlist({ payload }:any, { call, put }:any):any {
   const {page,pagesize}=payload
   const result:any=yield call(getlist,page,pagesize)
   yield put({
    type:"setlist",
    payload:result.data.data
   })
    },
    //评论
    *getcomment({ payload }:any, { call, put }:any):any {
      console.log(payload);
   const result:any=yield call(getcomment)
   console.log(result);
   yield put({
    type:"setcomment",
    payload:result.data.data
   })
    },
    //通过未通过
    *showes({payload}:any,{call,put}:any):any{
      const {id,pass}=payload;
      const result=yield call(commshow,id,pass)
      console.log(result.data.data);

      yield put({
        type:"setshowes",
        payload:result.data.data
      })
    },
    //评论删除
    *delete({payload}:any,{call,put}:any):any{
      console.log(payload,'shanchu1111111111111');

      const {id}=payload;
      const result=yield call(getdelete,id)
      console.log(result);

      yield put({
        type:"setdel",
        payload:id
      })
      if(result.state===200){
        message.success('删除成功')
      }
    },
    //文章删除
    *editdel({payload}:any,{call,put}:any):any{
      console.log(payload,'shanchu1111111111111');
      const result=yield call(editdel,payload)
      console.log(result);

      yield put({
        type:"seteditdel",
        payload:payload
      })
      if(result.state===200){
        message.success('删除成功')
      }
    },
    //回复添加
    *push({payload}:any,{call,put}:any):any{
       console.log(payload,'0000000000000000000000');

      const result=yield call(getpush,payload)
      console.log(result);

      yield put({
        type:"setpush",
        payload:result.data.data
      })
      if(result.status===201){
        message.success('回复成功')
      }
    },
    *edit({payload}:any,{call,put}:any):any{
       console.log(payload,'0000000000000000000000');
      const {id,content,title}=payload
      const result=yield call(getedit,payload)
      console.log(result);

      yield put({
        type:"setedit",
        payload:result.data.data
      })
      if(result.status===200){
        message.success('文章已发布')
      }
    },
  },
  reducers: {
    //列表
    setlist(state:any, action:any) {
      state.list=action.payload[0]
      return {
        ...state,
        ...action.payload,
      };
    },
    //评论
    setcomment(state:any, action:any) {
      console.log(action.payload,'111111111111111');
      state.comm=action.payload[0]
      return {
        ...state,
        ...action.payload,
      };
    },
    //通过未通过
    setshowes(state:any, action:any) {
      console.log(action.payload);

      state.comm.forEach((item:any,index:number)=>{
        if(item.id===action.payload.id){
          item.pass =! item.pass
        }
      })
      return {
        ...state,
        ...action.payload,
      };
    },
    //删除
    setdel(state:any, action:any) {
      state.comm.forEach((item:any,index:number)=>{
        if(item.id===action.payload){
         state.comm.splice(index,1)
        }
      })
      return {
        ...state,
        ...action.payload,
      };
    },
    //文章删除
    seteditdel(state:any, action:any) {
      state.list.forEach((item:any,index:number)=>{
        if(item.id===action.payload){
         state.list.splice(index,1)
        }
      })
      return {
        ...state,
        ...action.payload,
      };
    },
    //添加
    setpush(state:any, action:any) {
      console.log(action);
    state.comm.unshift(action.payload)

      return {
        ...state,
        ...action.payload,
      };
    },
    //编辑
    setedit(state:any, action:any) {
      state.list.forEach((item:any,index:number)=>{
        if(item.id===action.payload.id){
          item.title=action.payload.title
          item.content=action.payload.content
        }
      })

      return {
        ...state,
        ...action.payload,
      };
    },

  },
};

export default IndexModel;
